<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodsController extends Controller
{
    //getAdd
    public function getAdd()
    {
    	$data = DB::table('cate')
    		-> select(['id', 'name', 'path', DB::raw('CONCAT(path,",",id) AS sortPath')])
    		-> orderBy('sortPath', 'ASC')
    		-> get();

    	//dd($data);

    	foreach ($data as $k => $v)
    	{
    		$count = substr_count($v -> path, ',');
    		$str = str_repeat('|----', $count);
    		$data[$k] -> name = $str.$v -> name; 
    	}
    	return view('admin.goods.add',['data' => $data]);
    }

    //insert
    public function postInsert(Request $request)
    {	
    	$data = $request -> except('_token');
        //处理brand_id  来上传品牌图片  
        $data['brand_id'] = $data['cate_id'];
        //dd($data);

    	//上传图片
    	if($request -> hasFile('img'))
    	{
    		if($request -> file('img') -> isValid())
    		{
    			//获取扩展名称
    			$suffix = $request -> file('img') -> getClientOriginalExtension();

    			//随机名称
    			$fileName =time().mt_rand(100000,999999).'.'.$suffix;

    			// echo $fileName;
    			
    			$request -> file('img') -> move('./uploads/goods/', $fileName);

    			//处理 img字段
		    	$data['img'] = '/uploads/goods/'.$fileName;
    		}
    	}

    	
    	//执行添加
    	$res = DB::table('goods') -> insert($data);
    	//判断
    	if($res)
    	{
    		return redirect('admin/goods/index') -> with(['info' => '添加成功']);
    	}else
    	{
    		return back() -> with(['error' => '添加失败']);
    	}

    }

    //index
    public function getIndex(Request $request)
    {

    	//查询
    	$data = DB::table('goods')
    		-> where(function($query) use ($request){
    			$query -> where('name', 'like', '%'.$request -> input('keyword').'%');
    		})
    		-> paginate($request -> input('num', 10));

    	return view('admin.goods.index',['data' => $data, 'request' => $request -> all()]);
    }

    //edit
    public function getEdit($id)
    {
    	$data = DB::table('goods') -> where('id', $id) -> first();
    	$cates = DB::table('cate') 
    		-> select(['id', 'name', 'path', DB::raw('CONCAT(path,",",id) AS sortPath')])
    		-> orderBy('sortPath', 'ASC')
    		-> get();

    	foreach ($cates as $k => $v)
    	{
    		$count = substr_count($v -> path, ',');
    		$str = str_repeat('|', $count);
    		$cates[$k] -> name = $str.$v -> name;
    	}
    	return view('admin.goods.edit', ['data' => $data, 'cates' => $cates]);
    }

    //update
    public function postUpdate(Request $request)
    {

    	$id = $request -> input('id');
    	$oldImg = DB::table('goods') -> where('id', $id) -> first() -> img;

    	// dd($oldImg);
    	$data = $request -> except('_token','id');

    	if($request -> hasFile('img')) 
    	{
    		if($request -> file('img') -> isValid())
    		{

    			//获取扩展名称
	    		$suffix = $request -> file('img') -> getClientOriginalExtension();

	    		//随机名称
	    		$fileName = time().mt_rand(100000,999999).'.'.$suffix;

	    		$request -> file('img') -> move('./uploads/goods/', $fileName);

	    		//处理 img 字段
	    		$data['img'] = '/uploads/goods/'.$fileName;

	    		if(file_exists('.'.$oldImg))
	    		{
	    			unlink('.'.$oldImg);
	    		}

    		}
    		
    	}

    	//更新

    	$res = DB::table('goods') -> where('id', $id) -> update($data);

	    //判断
	    if($res)
	    {
	    	return redirect('admin/goods/index') -> with(['info' => '更新成功']);
	    }else
	    {
	    	return back() -> with(['error' => '更新失败']);
	    }
    	
    }

    //delete
    public function getDelete($id)
    {
    	$res = DB::table('goods') -> where('id', $id) -> update(['is_delete' => 0]);

		if($res)
		{
			return redirect('admin/goods/index') -> with(['info' => '下架成功']);
		}else
		{
			return back() -> with(['error' => '下架失败']);
		}
    }
 		
 	//ajaxUpdate
   /* public function postAjaxUpdate(Request $request)
    {

    	return response() -> json(2);

    	$id = $request -> input('id');
    	//根据ID查询数据
    	$data = DB::table('goods') -> where('id', $id) -> first();

    	//判断
    	if($data -> status == '0')
    	{
    		$res = DB::table('goods') -> where('id', $id) -> update(['is_delete' => '1']);
    		if($res)
    		{
    			return response() -> json(0);
    		}else
    		{
    			return response() -> json(1);
    		}
    	}else
    	{
    		$res = DB::table('goods') -> where('id', $id) -> update(['is_delete' => '0']);
    		if($res)
    		{
    			return response() -> json(2);
    		}else
    		{
    			return response() -> json(3);
    		}
    	}


    }

    //ajaxUsername
    public function postAjaxUsername(Request $request)
    {
    	$name = $request -> input('name');
    	//查询
    	$res = DB::table('goods') -> where('name',$name) -> first();
    	if($res)
    	{
    		//1代表用户名已存在
    		return response() -> json(1);
    	}else
    	{
    		$res = DB::table('goods') -> where('id',$request -> input('id')) -> update(['name' => $name]);

    		if($res)
    		{
    			return response() -> json(0);
    		}else
    		{
    			return response() -> json(2);
    		}
    	}

    }*/
}
